/**
 * Created by wangwj on 2016/11/30.
 */
var gulp = require('gulp');
var assetRev = require('leading-gulp-asset-rev');
var del = require("del");
var jshint = require('gulp-jshint');
var minifycss = require('gulp-minify-css');
var es = require('event-stream');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var runSequence = require('run-sequence');
var zip = require('gulp-zip');
var bowerJson = require("./bower.json");
var timeStr = new Date().getTime().toString();

gulp.task('clean', function () {
    del(["./dist"]);
    del.sync('./dist', {force: true});
});

gulp.task("copy", function () {
    gulp.src('./data/**/*.*')
        .pipe(gulp.dest("./dist/data"));
    gulp.src('./assets/**/*.*')
        .pipe(gulp.dest("./dist/assets"));
    gulp.src(['./*.ico', './bower.json'])
        .pipe(gulp.dest("./dist"));
});

gulp.task("buildJs", function () {
    var stream = gulp.src(['./js/**/*.js', './js/**/*.html'])
        .pipe(gulp.dest("./dist/js"))
        .on('end', function () {
            gulp.src('./dist/js/**/*.js')
                .pipe(jshint())
                .pipe(assetRev({verStr: "?t=" + timeStr}))
                .pipe(jshint())
                .pipe(uglify({
                    mangle: false
                }))
                .pipe(gulp.dest("./dist/js"));
        });
    return stream;
});

gulp.task("buildHtml", function () {
    gulp.src('./views/**/*.html')
        .pipe(assetRev({verStr: "?t=" + timeStr}))
        .pipe(gulp.dest("./dist/views"));

    gulp.src('./tpl/**/*.html')
        .pipe(assetRev({verStr: "?t=" + timeStr}))
        .pipe(gulp.dest("./dist/tpl"));

    gulp.src('./*.html')
        .pipe(assetRev({verStr: "?t=" + timeStr}))
        .pipe(gulp.dest("./dist"));

});

gulp.task("release", function () {
    var name = bowerJson.name;
    var version = bowerJson.version;
    gulp.src('./dist/**')
        .pipe(zip(name + version + '_release_' + timeStr + '.zip'))
        .pipe(gulp.dest('./dist'));
});

gulp.task('default', function () {
    runSequence('clean', 'copy', 'buildHtml', 'buildJs');
});